import { User } from '../../../db/models/user'
import {sendSuccess,sendError } from "../../utils/responseHandler";

export default async function handler(req, res) {
    if (req.method === 'GET') {
        try {
            const users = await User.findAll();
            sendSuccess(res, users);
        } catch (error) {
            console.error('获取失败:', error);
            sendError(res, '获取失败', 500);
        }
    } else if (req.method === 'POST') {
        try {
            const { username, nickname, email,work_position,year } = req.body;
            const defaultPassword = '123456';  // 设置默认密码
            const avatar = 'https://api.dicebear.com/7.x/miniavs/svg?seed=20'
            await User.create({
                username,
                nickname,
                email,
                work_position,
                year,
                avatar,
                password: defaultPassword  // 使用默认密码
            });
            sendSuccess(res, '1');
        } catch (error) {
            console.error('创建失败:', error);
            sendError(res, '创建失败', 500);
        }
    } else {
        res.setHeader('Allow', ['GET', 'POST']);
        res.status(405).end(`Method ${req.method} Not Allowed`);
    }
}
